<?php
/**
 * Introduction: 管理员操作
 * @author: 杨陈鹏
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Admin\Controller\Api;


use Common\Controller\Admin\CommonController;

class IndexController extends CommonController
{
    /**
     * Introduction: 获取分类或者商品列表 百度编辑器设置图片属性用
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function setUeditorImgAttr()
    {

        $action = I('post.action');

        $logic = D('Common/Category', 'Logic');
        //获取所有规则
        $categorys = $logic::getChilds(0, 'id,name,pid,child');

        switch($action)
        {

            case 'category':


                //获取树型下拉框
                $tree = new \Org\Util\Tree();

                $tree->init($categorys);

                $this->category_tree = '<div class="col-sm-12" style="margin-top:10px;"><select name="category_id" class="form-control radius-none">';

                $this->category_tree .= $tree->create(0, '<option value=$id data-name=$name>$spacer $name</option>');


                $this->category_tree .= '</select></div>';
                //$this->display();

                echo $this->category_tree;

                break;


            case 'goods':


                $goods_logic = D('Common/Goods','Logic');


                $categoryLogic = D('Common/category', 'Logic');

                $mothed = I('post.mothed');


                $category_id = intval(I('post.category_id'));

                $this->page = I('param.page',1);


                $where = '';


                //如果有分类ID
                if ($category_id) {

                    //获取指定分类下的所有子分类ID

                    $category_ids = $categoryLogic::getChilds($category_id, 'id');

                    $category_ids_str = '';
                    foreach ($category_ids as $v)
                        $category_ids_str .= $v['id'] . ',';

                    $category_ids_str = $category_ids_str . $category_id;

                    $category_ids_str = rtrim($category_ids_str, ',');

                    $where .= "goods.category_id IN($category_ids_str) AND";

                }

                $whr = "extend.is_default = 1";

                $fileds = "extend.goods_id,goods.title,extend.thumb,extend.goods_price";

                $this->goods_info = $goods_logic::$model
                                ->alias('goods')
                                //->fetchSql(true)
                                ->join('__GOODS_EXTEND__ extend ON goods.id = extend.goods_id', 'LEFT')
                                ->where("$where $whr")
                                ->field($fileds)
                                ->page($this->page,20)
                                ->select();


                //获取树型下拉框
                $tree = new \Org\Util\Tree();

                $tree->init($categorys);

                $this->category_tree = $tree->create(0, '<option value=$id $disabled>$spacer $name</option>');


                if($mothed == 'ajax'){

                    $str = '';

                    foreach($this->goods_info as $v){

                        $thumb = cutUrl($v['thumb'],50,50);
                        $goods_price = sprintf("%.2f", ($v['goods_price'] / 100));
                    $str .= <<<str
            <tr>
                <td style="line-height: 50px">
                    <input type="radio" data-price="{$v['goods_price']}" name="goods_id" value="{$v['goods_id']}">
                </td>
                <td>
                    <img src="{$thumb}" width="50px" height="50px">
                </td>
                <td style="line-height: 50px">{$v['goods_id']}</td>
                <td style="line-height: 50px">{$v['title']}</td>
                <td style="line-height: 50px">{$goods_price}</td>
            </tr>
str;
                    }
                    echo $str;


                }else{

                    $this->display();

                }


                break;


        }

    }


}